package com.chen10.gobang.controller;

import com.chen10.gobang.common.enums.response.SuccessMsgEnum;
import com.chen10.gobang.common.exception.BusinessException;
import com.chen10.gobang.common.protocal.ResponseResult;
import com.chen10.gobang.common.protocal.ResponseVO;
import com.chen10.gobang.model.dto.ForgetPwdDTO;
import com.chen10.gobang.model.dto.UpdateUsernameDTO;
import com.chen10.gobang.model.dto.UserRankingPageDTO;
import com.chen10.gobang.model.page.Pager;
import com.chen10.gobang.model.vo.UserInfoVO;
import com.chen10.gobang.model.vo.UserRankingVO;
import com.chen10.gobang.model.vo.UserSearchVO;
import com.chen10.gobang.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 修改密码
     * @param forgetPwdDTO
     * @return
     */
    @PostMapping("/forgetPwd")
    public ResponseVO forgetPwd(@RequestBody ForgetPwdDTO forgetPwdDTO){
        log.info("用户忘记密码，forgetPwdDTO：{}",forgetPwdDTO);
        try {
            userService.updatePassword(forgetPwdDTO);
            return ResponseResult.success(SuccessMsgEnum.UPDATE_PASSWORD_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 搜索用户
     * 根据用户输入返回用户搜索结果
     * @param param 可能是email也可能是username
     * @return 返回用户搜索结果
     */
    @GetMapping("/search/{param}")
    public ResponseVO<List<UserSearchVO>> getUsers(@PathVariable String param){
        log.info("搜索用户，参数param：{}",param);
        List<UserSearchVO> users = null;
        try{
           users = userService.getUsers(param);
           return ResponseResult.success(users);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 获取用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    public ResponseVO<UserInfoVO> getUserInfo(){
        try{
            return ResponseResult.success(userService.getUserInfo());
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 更新用户名
     * @param updateUsernameDTO
     * @return
     */
    @PostMapping("/updateUsername")
    public ResponseVO updateUsername(@RequestBody UpdateUsernameDTO updateUsernameDTO){
        try{
            userService.updateUsername(updateUsernameDTO);
            return ResponseResult.success(SuccessMsgEnum.UPDATE_USERNAME_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 更新头像
     * @param file 头像文件
     * @return 新头像url
     */
    @PostMapping("/updateAvatar")
    public ResponseVO<String> updateAvatar (@RequestParam("avatarFile") MultipartFile file) {
        try{
            return ResponseResult.success(SuccessMsgEnum.UPDATE_AVATAR_SUCCESS.getMsg(),
                    userService.updateAvatar(file));
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 分页查询排行榜
     * @param pageDTO
     * @return
     */
    @PostMapping("/ranking/page")
    public ResponseVO<Pager<UserRankingVO>> getRankingPage(@RequestBody UserRankingPageDTO pageDTO){
        try{
            return ResponseResult.success(userService.getRankingPage(pageDTO));
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }
}
